//
//  QiuBaiVC.m
//  guess
//
//  Created by apple on 15/11/5.
//  Copyright © 2015年 fank. All rights reserved.
//

#import "QiuBaiVC.h"
#import "Define.h"
#import "HYSegmentedControl.h"
#import "QiuBaiSingleDataModel.h"
#import "QiuBaiCell.h"



@interface QiuBaiVC()<UITableViewDataSource,UITableViewDelegate>
{
    int page1;
}
@property (strong, nonatomic)HYSegmentedControl *segmentedControl;
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *tableViewDatasource;
@property (strong,nonatomic) NSMutableArray *tableViewDatasource11;
@end

@implementation QiuBaiVC
static NSString *CellIdentifier = @"reusable";
-(void)createNaviBar
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kscreenWith, 64)];
    titleView.backgroundColor = UIColorFromRGBValue(0xf3f3f3);
    self.segmentedControl = [[HYSegmentedControl alloc] initWithOriginY:64-36 Titles:@[@"搞笑视频"] delegate:self];
    [titleView addSubview:_segmentedControl];
    [self.view addSubview:titleView];
    
    //背景颜色
    self.view.backgroundColor = UIColorFromRGBValue(0xf0ffff);
}

-(void)initSubView
{
    [self createNaviBar];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kscreenWith, kscreenHeight-64-49)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[QiuBaiCell class] forCellReuseIdentifier:CellIdentifier];
    [self.view addSubview:self.tableView];
    
    NSLog(@"%@",kDocumentPath);
    
    //集成刷新控件
    [self addHeader];
    [self addFooter];
}

- (void)addHeader
{
    __unsafe_unretained typeof(self) vc = self;
    // 添加下拉刷新头部控件
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
         page1 = 1;
         [vc getQiuBaiVideoQequest];
    }];
    [self.tableView.header beginRefreshing];
}

- (void)addFooter
{
    __unsafe_unretained typeof(self) vc = self;
    // 添加上拉刷新尾部控件
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page1 +=1;
        [vc getQiuBaiVideoQequest];
    }];
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    _tableViewDatasource11 = [NSMutableArray array];
    for (int i =0; i < 200; i++) {
        [_tableViewDatasource11 addObject:@(i)];
    }
    
    
    [self initSubView];
    
}

#pragma mark-
#pragma mark-网络请求
-(void)getQiuBaiVideoQequest
{
   // 糗百视频
    
    [DejalActivityView activityViewForView:self.view];
    NSString *urlString = QiuBai_API;
    [GHttpEnqine getQiuBaiVideoWithUrl:urlString page:@(page1) success:^(id data) {
        NSLog(@"%@",data);
        
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        
        NSMutableArray *array = [NSMutableArray array];
        
        for (NSDictionary *temp in data) {
            NSError *error;
            QiuBaiSingleDataModel *qiuBaiData = [QiuBaiSingleDataModel modelWithDictionary:temp error:&error];
            [array addObject:qiuBaiData];
        }

        if (!_tableViewDatasource) {
            _tableViewDatasource = [NSMutableArray array];
        }
        if (page1 == 1) {
            [_tableViewDatasource removeAllObjects];
            _tableViewDatasource = array;
        }else if(page1 > 1){
            for (QiuBaiSingleDataModel *temp in array) {
                [_tableViewDatasource addObject:temp];
            }
        }
        [self.tableView reloadData];
        [DejalActivityView removeView];
    } fail:^(id error) {
        [DejalActivityView removeView];
        [ABTools showToastView:@"加载失败" onView:self.view];
    }];
}

#pragma mark-
#pragma mark-tableViewDatasource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * labelStr = [_tableViewDatasource[indexPath.row] title];
    CGSize labelSize = {0, 0};
    labelSize = [labelStr sizeWithFont:[UIFont systemFontOfSize:18]
                     constrainedToSize:CGSizeMake(kscreenWith - 30, 5000)
                         lineBreakMode:NSLineBreakByWordWrapping];
    return kscreenWith + 95 + labelSize.height;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableViewDatasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QiuBaiCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[QiuBaiCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    QiuBaiSingleDataModel *qiuBaiData = _tableViewDatasource[indexPath.row];
    cell.headImageUrl = qiuBaiData.headImage;
    cell.autherName = qiuBaiData.autherName;
    cell.title = qiuBaiData.title;
    cell.poster = qiuBaiData.poster;
    cell.videoUrl = qiuBaiData.videoUrl;
    cell.backgroundColor = RGBCOLOR(241, 241, 241);
    cell.tag = indexPath.row;
    [[cell valueForKey:@"_shareBtn"] addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    
//    cell.shareBlock = ^(NSInteger index){
//        [self shareFunWithShareInfo1:index];
//    };
    return cell;
}

-(void)share:(UIButton *)sender
{
    [self shareFunWithShareInfo1:sender.tag];
}

//分享方法
-(void)shareFunWithShareInfo1:(NSInteger )row
{
    NSIndexPath *index = [NSIndexPath indexPathForRow:row inSection:0];
    QiuBaiCell *cell = [self.tableView cellForRowAtIndexPath:index];
    QiuBaiSingleDataModel *qiuBaiData = _tableViewDatasource[row];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    [dictionary setObject:[[cell valueForKey:@"_posterImageView"] valueForKey:@"image"] forKey:@"image"];
    [dictionary setObject:qiuBaiData.videoUrl forKey:@"url"];
    [dictionary setObject:qiuBaiData.title forKey:@"title"];
    
    BOOL isInstallWeChat = [WXApi isWXAppInstalled];
    BOOL isInstallQQ    = [QQApiInterface isQQInstalled];
    
    NSMutableArray *array = [NSMutableArray array];
    if (isInstallWeChat == YES) {
        [array addObject:UMShareToWechatFavorite];
        [array addObject:UMShareToWechatSession];
        [array addObject:UMShareToWechatTimeline];
    }
    [array addObject:UMShareToSina];
    if (isInstallQQ == YES) {
        [array addObject:UMShareToQQ];
        [array addObject:UMShareToQzone];
    }
    if (dictionary) {
        
        UIImage *image = [dictionary objectForKey:@"image"];
        NSString *url1 = [dictionary objectForKey:@"url"];
//        NSString *url1 = [dictionary objectForKey:@"www.baidu.com"];
        NSString *title = [dictionary objectForKey:@"title"];
            //微信收藏
        [UMSocialData defaultData].extConfig.wechatFavoriteData.shareImage = image;
        [UMSocialData defaultData].extConfig.wechatFavoriteData.title = title;
        [UMSocialData defaultData].extConfig.wechatFavoriteData.url = url1;
        [UMSocialData defaultData].extConfig.wechatFavoriteData.wxMessageType = UMSocialWXMessageTypeWeb;
            //微信好友
        [UMSocialData defaultData].extConfig.wechatSessionData.shareImage = image;
        [UMSocialData defaultData].extConfig.wechatSessionData.title = title;
        [UMSocialData defaultData].extConfig.wechatSessionData.url = url1;
        [UMSocialData defaultData].extConfig.wechatSessionData.wxMessageType = UMSocialWXMessageTypeWeb;
            //微信朋友圈
        [UMSocialData defaultData].extConfig.wechatTimelineData.shareImage = image;
        [UMSocialData defaultData].extConfig.wechatTimelineData.title = title;
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = url1;
        [UMSocialData defaultData].extConfig.wechatTimelineData.wxMessageType = UMSocialWXMessageTypeWeb;
            //新浪微博
        [UMSocialData defaultData].extConfig.sinaData.shareImage = image;
        UMSocialUrlResource *urlResource = [[UMSocialUrlResource alloc]init];
        urlResource.url = url1;
        [UMSocialData defaultData].extConfig.sinaData.urlResource = urlResource;
        //QQ
        [UMSocialData defaultData].extConfig.qqData.shareImage = image;
        [UMSocialData defaultData].extConfig.qqData.url = url1;
        [UMSocialData defaultData].extConfig.qqData.title = title;
        [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            //QQ空间
        [UMSocialData defaultData].extConfig.qzoneData.shareImage = image;
        [UMSocialData defaultData].extConfig.qzoneData.url = url1;
        [UMSocialData defaultData].extConfig.qzoneData.title = title;
        }
        [UMSocialSnsService presentSnsIconSheetView:self appKey:UmengAppkey shareText:qiuBaiData.title shareImage:nil shareToSnsNames:array delegate:self];
}

//实现回调方法（可选）：
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}



@end
